package com.jzlin.funhub.admin.service.file.strategy;

import cn.hutool.core.io.FileUtil;
import com.jzlin.funhub.admin.common.enums.FileStorageStrategyEnum;
import com.jzlin.funhub.admin.config.properties.FileStorageProperties;
import com.jzlin.funhub.admin.model.dto.FileUploadResultDTO;
import com.jzlin.funhub.framework.core.exception.FileStorageException;
import lombok.Getter;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

/**
 * 本地存储文件.
 *
 * @author linjz
 * @since 2025/8/10
 */
@Getter
public class LocalFileStoreStrategy implements IFileStorageStrategy {
    private final String rootPath;

    public LocalFileStoreStrategy(FileStorageProperties properties) {
        this.rootPath = properties.getRootPath();
    }


    @Override
    public FileUploadResultDTO upload(File file, String descPath) throws FileStorageException {
        FileUploadResultDTO resultDTO = new FileUploadResultDTO();
        resultDTO.setStoragePath(descPath);
        try {
            File destFile = new File(getAbsolutePath(descPath));
            if (!destFile.getParentFile().exists() && !destFile.getParentFile().mkdirs()) {
                throw new FileStorageException("文件上传失败:创建父目录失败");
            }
            Files.copy(file.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

            resultDTO.setOriginalFilename(file.getName());
            resultDTO.setFileExtension(FileUtil.extName(file));
            resultDTO.setFileSize(file.length());
            resultDTO.setStrategy(getStrategy().name());
            return resultDTO;
        } catch (IOException e) {
            throw new FileStorageException("文件上传失败", e);
        }
    }

    @Override
    public FileUploadResultDTO upload(MultipartFile file, String descPath) throws FileStorageException {
            FileUploadResultDTO resultDTO = new FileUploadResultDTO();
            resultDTO.setStoragePath(descPath);
            try {

                File destFile = new File(getAbsolutePath(descPath));
                if (!destFile.getParentFile().exists() && !destFile.getParentFile().mkdirs()) {
                    throw new FileStorageException("文件上传失败:创建父目录失败");
                }
                file.transferTo(destFile);

                resultDTO.setOriginalFilename(file.getOriginalFilename());
                resultDTO.setFileExtension(FileUtil.extName(file.getOriginalFilename()));
                resultDTO.setFileSize(file.getSize());
                resultDTO.setStrategy(getStrategy().name());
                return resultDTO;
            } catch (IOException e) {
                throw new FileStorageException("文件上传失败", e);
            }
    }

    private String getAbsolutePath(String descPath) {
        descPath = descPath.replace("/", File.separator);
        if (descPath.startsWith(File.separator)) {
            descPath = descPath.substring(1);
        }
        if (rootPath.endsWith(File.separator)) {
            return rootPath + descPath;
        }
        return rootPath + File.separator + descPath;
    }

    @Override
    public InputStream download(String path) throws FileStorageException {
        try {
            File file = new File(rootPath + File.separator + path.replace("/", File.separator));
            return new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new FileStorageException("文件不存在: " + path, e);
        }
    }

    @Override
    public void delete(String path) throws FileStorageException {

    }

    @Override
    public FileStorageStrategyEnum getStrategy() {
        return FileStorageStrategyEnum.LOCAL;
    }
}
